450. 删除二叉搜索树中的节点
为保证权益,题目请参考 450. 删除二叉搜索树中的节点(From LeetCode).
解决方案1
Python
python
# 450. 删除二叉搜索树中的节点
# https://leetcode.cn/problems/delete-node-in-a-bst/
from typing import Optional, Tuple
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def deleteNode(self, root, key: int):
def findNode(root, key: int):
if root is not None:
if root.val == key:
return None, root
else:
a = (None, None)
if root.val < key:
a = findNode(root.right, key)
else:
a = findNode(root.left, key)
if a[1] is not None and a[0] is None:
a = (root, a[1])
return a
else:
return None, None
par, node = findNode(root, key)
if node is None:
return root
def mergeBinTree(left: TreeNode, right: TreeNode) -> TreeNode:
if left is None:
if right is None:
return None
else:
return right
else:
if right is None:
return left
else:
right.left = mergeBinTree(left, right.left)
return right
if par is None:
return mergeBinTree(node.left, node.right)
else:
if par.val > node.val:
par.left = mergeBinTree(node.left, node.right)
else:
par.right = mergeBinTree(node.left, node.right)
del node
return root
if __name__ == "__main__":
so = Solution()
node5 = TreeNode(5)
node2 = TreeNode(2)
node3 = TreeNode(3)
node4 = TreeNode(4)
node5.left = node3
node3.left = node2
node3.right = node4
print(so.deleteNode(node5, 3))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74